package controllers

import (
"encoding/json"

"gitee.com/lzqysoft/ims/models"


"github.com/astaxie/beego"
)

// ServController operations for Serv
type ServController struct {
	CommonController
}

// URLMapping ...
func (c *ServController) URLMapping() {
	c.Mapping("Post", c.Post)
	c.Mapping("GetOne", c.GetOne)
	c.Mapping("GetAll", c.GetAll)
	c.Mapping("Put", c.Put)
	c.Mapping("Delete", c.Delete)
	c.Mapping("GetTimeLine",c.GetTimeLine)
}

// Post ...
// @Title Post
// @Description create Serv
// @Param	body		body 	models.Serv	true		"body for Serv content"
// @Success 201 {int} models.Serv
// @Failure 403 body is empty
// @router / [post]
func (c *ServController) Post() {
	var v models.Serv
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
		if _, err := models.AddServ(&v); err == nil {
			c.Ctx.Output.SetStatus(201)
			c.Data["json"] = v
		} else {
			c.Data["json"] = err.Error()
		}
	} else {
		c.Data["json"] = err.Error()
	}
	c.ServeJSON()
}

// GetOne ...
// @Title Get One
// @Description get Serv by id
// @Param	id		path 	string	true		"The key for staticblock"
// @Success 200 {object} models.Serv
// @Failure 403 :id is empty
// @router /GetOne [post]
func (c *ServController) GetOne() {
	sdata, err := c.AesDecode(c.GetString("tdata"))
	if err != nil {
		c.RetError(ErrInputData)
	}
	fdata, ok := sdata.(map[string]interface{})
	if !ok {
		c.RetError(ErrInputData)
	}
	id, ok := fdata["servId"].(string)
	if !ok {
		c.RetError(ErrInputData)
	}

	if v, err := models.GetServById(id); err != nil {
		beego.Debug(err)
		c.RetError(ErrDatabase)
	} else  {
		c.RetSuccess(v)
	}
}

// GetAll ...
// @Title Get All
// @Description get Serv
// @Param	query	query	string	false	"Filter. e.g. col1:v1,col2:v2 ..."
// @Param	fields	query	string	false	"Fields returned. e.g. col1,col2 ..."
// @Param	sortby	query	string	false	"Sorted-by fields. e.g. col1,col2 ..."
// @Param	order	query	string	false	"Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..."
// @Param	limit	query	string	false	"Limit the size of result set. Must be an integer"
// @Param	offset	query	string	false	"Start position of result set. Must be an integer"
// @Success 200 {object} models.Serv
// @Failure 403
// @router /GetAll [post]
func (c *ServController) GetAll() {
	sdata, err := c.AesDecodeToByte(c.GetString("tdata"))
	var pageData QueryParams
	if err = json.Unmarshal(sdata, &pageData);err != nil {
		beego.Debug(err)
		c.RetError(ErrInputData)
	}

	l, err := models.GetAllServ(pageData.Query, pageData.Fields, pageData.SortBy, pageData.Order, pageData.Page, pageData.PageSize)
	if err != nil {
		beego.Debug(err)
		c.RetError(ErrDatabase)
	}
	data := make(map[string]interface{})
	data["data"] = l
	data["total"] = len(l)
	c.RetSuccess(data)
}

// Put ...
// @Title Put
// @Description update the Serv
// @Param	id		path 	string	true		"The id you want to update"
// @Param	body		body 	models.Serv	true		"body for Serv content"
// @Success 200 {object} models.Serv
// @Failure 403 :id is not int
// @router /:id [put]
func (c *ServController) Put() {
	idStr := c.Ctx.Input.Param(":id")
	//id, _ := strconv.Atoi(idStr)
	v := models.Serv{Id: idStr}
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &v); err == nil {
		if err := models.UpdateServById(&v); err == nil {
			c.Data["json"] = "OK"
		} else {
			c.Data["json"] = err.Error()
		}
	} else {
		c.Data["json"] = err.Error()
	}
	c.ServeJSON()
}

// Delete ...
// @Title Delete
// @Description delete the Serv
// @Param	id		path 	string	true		"The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /:id [delete]
func (c *ServController) Delete() {
	idStr := c.Ctx.Input.Param(":id")
	//id, _ := strconv.Atoi(idStr)
	if err := models.DeleteServ(idStr); err == nil {
		c.Data["json"] = "OK"
	} else {
		c.Data["json"] = err.Error()
	}
	c.ServeJSON()
}

// GetTimeLine ...
// @Title GetTimeLine
// @Description GetTimeLine
// @Param	id		path 	string	true		"The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /GetTimeLine [post]
func (c *ServController)GetTimeLine()  {
	sdata,err := c.AesDecode(c.GetString("tdata"))
	if err != nil {
		c.RetError(ErrInputData)
	}
	fdata,ok := sdata.(map[string]interface{})
	if !ok {
		c.RetError(ErrInputData)
	}
	servId ,ok := fdata["servId"].(string)
	if !ok {
		c.RetError(ErrInputData)
	}
	serv,err := models.GetServById(servId)
	if err != nil {
		beego.Debug(err)
		c.RetError(ErrDatabase)
	}
	item,err := models.GetImplementDetailById(serv.ImplementDetailId)
	if err != nil {
		beego.Debug(err)
		c.RetError(ErrDatabase)
	}
	services, err := models.GetServicesByServId(serv.Id)
	if err != nil {
		beego.Debug(err)
		c.RetError(ErrDatabase)
	}
	res := make([]map[string]interface{},0)
	out := make(map[string]interface{})
	out["time"] = item.Ctime
	out["name"] = "出库"
	out["user"] = item.Creater
	res = append(res, out)
	set := make(map[string]interface{})
	set["time"] = item.SetTime
	set["name"] = "调试"
	set["user"] = item.Seter
	res = append(res, set)
	install := make(map[string]interface{})
	install["time"] = item.InstallTime
	install["name"] = "安装"
	install["user"] = item.Installer
	res = append(res, install)
	for _,v :=  range services {
		node := make(map[string]interface{})
		node["time"] = v.Ctime
		node["name"] = v.Name
		node["user"] = v.ResolveUser
		res = append(res, node)
	}
	c.RetSuccess(res)
}